#include<iostream>
#include<cstdlib>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<sys/select.h>
#include<sys/epoll.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<netdb.h>
#include<pthread.h>
#include<queue>
#include<errno.h>
#include<pthread.h>
using namespace std;
/********************************
	author:chenxuan
	date:2021/8/9
	funtion:an online disk server code 
*********************************/
using namespace std;
class ThreadPoll{
public:
	struct Task{
		void* (*ptask)(void*);
		void* arg;
	};
private:
	queue<Task> thingWork;
	pthread_cond_t condition;
	pthread_mutex_t lockPoll;
	pthread_mutex_t lockTask;
	pthread_mutex_t lockBusy;
	pthread_t* thread;
	pthread_t threadManager;
	unsigned int liveThread;
	unsigned int busyThread;
	bool isContinue;
private:
	static void* worker(void* arg)
	{
		ThreadPoll* poll=(ThreadPoll*)arg;
		while(1)
		{
			pthread_mutex_lock(&poll->lockPoll);
			while(poll->isContinue==true&&poll->thingWork.size()==0)
				pthread_cond_wait(&poll->condition,&poll->lockPoll);
			if(poll->isContinue==false)
			{
				pthread_mutex_unlock(&poll->lockPoll);
				pthread_exit(NULL);
			}
			if(poll->thingWork.size()>0)
			{
				pthread_mutex_lock(&poll->lockBusy);
				poll->busyThread++;
				pthread_mutex_unlock(&poll->lockBusy);
				ThreadPoll::Task task=poll->thingWork.front();
				poll->thingWork.pop();
				pthread_mutex_unlock(&poll->lockPoll);
				task.ptask(task.arg);
				pthread_mutex_lock(&poll->lockBusy);
				poll->busyThread--;
				pthread_mutex_unlock(&poll->lockBusy);
			}
		}
		return NULL;
	}
	static void* manager(void* arg)
	{
		return NULL;
	}
public:
	ThreadPoll(unsigned int threadNum=10)
	{
		if(threadNum<=1)
			threadNum=10;
		thread=new pthread_t[threadNum];
		if(thread==NULL)
			exit(0);
		for(unsigned int i=0;i<threadNum;i++)
			thread[i]=0;
		pthread_cond_init(&condition,NULL);
		pthread_mutex_init(&lockPoll,NULL);
		pthread_mutex_init(&lockTask,NULL);
		pthread_mutex_init(&lockBusy,NULL);
		liveThread=threadNum;
		isContinue=true;
		busyThread=0;
		for(unsigned int i=0;i<threadNum;i++)
			pthread_create(&thread[i],NULL,worker,this);
	}
	~ThreadPoll()
	{
		if(isContinue==false)
			return;
		isContinue=false;
//		pthread_join(threadManager,NULL);
		for(unsigned int i=0;i<liveThread;i++)
			pthread_cond_signal(&condition);
		for(unsigned int i=0;i<liveThread;i++)
			pthread_join(thread[i],NULL);
		pthread_cond_destroy(&condition);
		pthread_mutex_destroy(&lockPoll);
		pthread_mutex_destroy(&lockTask);
		pthread_mutex_destroy(&lockBusy);
		delete[] thread;
	}
	void threadExit()
	{
		pthread_t pid=pthread_self();
		for(unsigned int i=0;i<liveThread;i++)
			if(pid==thread[i])
			{
				thread[i]=0;
				break;
			}
		pthread_exit(NULL);
	}
	void addTask(Task task)
	{
		pthread_mutex_lock(&this->lockPoll);
		this->thingWork.push(task);
		pthread_mutex_unlock(&this->lockPoll);
		pthread_cond_signal(&this->condition);
	}
	void endPoll()
	{
		isContinue=false;
//		pthread_join(threadManager,NULL);
		for(unsigned int i=0;i<liveThread;i++)
			pthread_cond_signal(&condition);
		for(unsigned int i=0;i<liveThread;i++)
			pthread_join(thread[i],NULL);
		pthread_cond_destroy(&condition);
		pthread_mutex_destroy(&lockPoll);
		pthread_mutex_destroy(&lockTask);
		delete[] thread;
	}
	void getBusyAndTask(unsigned int* pthread,unsigned int* ptask)
	{
		pthread_mutex_lock(&lockBusy);
		*pthread=busyThread;
		pthread_mutex_unlock(&lockBusy);
		pthread_mutex_lock(&lockPoll);
		*ptask=thingWork.size();
		pthread_mutex_unlock(&lockPoll);
	}
	inline void mutexLock()
	{
		pthread_mutex_lock(&this->lockTask);
	}
	inline void mutexUnlock()
	{
		pthread_mutex_unlock(&this->lockTask);
	}
};
class ServerTcpIp{
private:
	int sizeAddr;//sizeof(sockaddr_in) connect with addr_in;
	int backwait;//the most waiting clients ;
	int numClient;//how many clients now; 
	int max;//the most clients;
	int fd_count;//sum of clients in fd_set
	int epfd;//file descriptor to ctrl epoll
	char* hostip;//host IP 
	char* hostname;//host name
	int sock;//file descriptor of host;
	int* psockClients;//client[];
	int sockC;//file descriptor to sign every client;
	epoll_event nowEvent;//a temp event to get event
	epoll_event* pevent;//all the event
	sockaddr_in addr;//IPv4 of host;
	sockaddr_in client;//IPv4 of client;
	fd_set  fdClients;//file descriptor
	ThreadPoll* pool;
	pthread_mutex_t mutex;
protected:
    int* pfdn;//pointer if file descriptor
    int fdNumNow;//num of fd now
    int fdMax;//fd max num
    bool addFd(int addsoc)//add file des criptor
    {
        bool flag=false;
        for(int i=0;i<fdNumNow;i++)
        {
            if(pfdn[i]==0)
            {
                pfdn[i]=addsoc;
                flag=true;//has free room
                break;
            }
        }
        if(flag==false)//no free room
        {
            if(fdNumNow>=fdMax)
            {
                pfdn=(int*)realloc(pfdn,sizeof(int)*fdMax+32);//try to realloc
                if(pfdn==NULL)
                	return false;
                fdMax+=10;
            }
            pfdn[fdNumNow]=addsoc;
            fdNumNow++;
        }
        return true;
    }
    bool deleteFd(int clisoc)//delete
    {
        for(int i=0;i<fdNumNow;i++)
        {
            if(pfdn[i]==clisoc)
            {
                pfdn[i]=0;
                return true;
            }
        }
        return false;
    }
public:
	struct ArgvSer{
		ServerTcpIp& server;
		int soc;
		void* pneed;
	};
public:
	ServerTcpIp(unsigned short port=5200,int epollNum=0,int wait=5,int maxClient=0)
	{
		sock=socket(AF_INET,SOCK_STREAM,0);//AF=addr family internet
		addr.sin_addr.s_addr=htonl(INADDR_ANY);//inaddr_any
		addr.sin_family=AF_INET;//af_intt IPv4
		addr.sin_port=htons(port);//host to net short
		fd_count=0;// select model
		sizeAddr=sizeof(sockaddr);
		backwait=wait;
		numClient=0;
		hostip=(char*)malloc(sizeof(char)*200);
		memset(hostip,0,sizeof(char)*200);
		hostname=(char*)malloc(sizeof(char)*300);
		memset(hostname,0,sizeof(char)*300);
		FD_ZERO(&fdClients);//clean fdClients;
		epfd=epoll_create(epollNum);
		if((pevent=(epoll_event*)malloc(512*sizeof(epoll_event)))==NULL)
			throw NULL;
		memset(pevent,0,sizeof(epoll_event)*512);
		memset(&nowEvent,0,sizeof(epoll_event));
        pfdn=(int*)malloc(sizeof(int)*64);
        if(pfdn==NULL)
            throw NULL;
        memset(pfdn,0,sizeof(int)*64);
        fdNumNow=0;
        fdMax=64;
		if(maxClient<=0||maxClient>100)
			psockClients=NULL;
		else 
		{
			psockClients=(int*)malloc(sizeof(int)*maxClient);
			if(psockClients==NULL)
				throw NULL;
			max=maxClient;
		}
		pool=new ThreadPoll(10);
		if(pool==NULL)
			throw NULL;
		pthread_mutex_init(&mutex,NULL);
	}
	~ServerTcpIp()//clean server
	{
		if(psockClients!=NULL)
			free(psockClients);
		close(sock);
		close(sockC);
		close(epfd);
		free(hostip);
		free(hostname);
		free(pevent);
        free(pfdn);
        free(pool);
        pthread_mutex_destroy(&mutex);
	}
	void mutexLock()
	{
		pthread_mutex_lock(&mutex);
	}
	void mutexUnlock()
	{
		pthread_mutex_unlock(&mutex);
	}
	bool mutexTryLock()
	{
		if(0==pthread_mutex_trylock(&mutex))
			return true;
		else
			return false;
	}
	bool bondhost()//bond myself first
	{
		if(bind(sock,(sockaddr*)&addr,sizeof(addr))==-1)
			return false;
		return true;
	}
	bool setlisten()//set listem to accept second
	{
		if(listen(sock,backwait)==-1)
			return false;
		FD_SET(sock,&fdClients);
		nowEvent.events=EPOLLIN;
		nowEvent.data.fd=sock;
		epoll_ctl(epfd,EPOLL_CTL_ADD,sock,&nowEvent);
		fd_count++;
		return true;
	}
	bool acceptClient()//wait until success model one
	{
		sockC=accept(sock,(sockaddr*)&client,(socklen_t*)&sizeAddr);
		return true;
	}
	bool acceptClientsModelTwo(int cliNum)//model two
	{
		if(cliNum<max)
		{
			psockClients[cliNum]=accept(sock,(sockaddr*)&client,(socklen_t*)&sizeAddr);
			numClient++;
		}	
		else
			return false;
		return true;
	}
	inline int receiveMystl(void* pget,int len)//model one
	{
		return recv(sockC,(char*)pget,len,0);
	}
	inline int receiveMysqlAllModel(int clisoc,void* pget,int len)
	{
		return recv(clisoc,(char*)pget,len,0);
	}
	int receiveSMystlModelTwo(void* prec,int cliNum,int len)//model two
	{
		int temp=recv(psockClients[cliNum],(char*)prec,len,0);
		if(temp==-1)
			psockClients[cliNum]=0;
		return temp;
	}
	inline int sendClientMystl(const void* ps,int len)//model one
	{
		return send(sockC,(char*)ps,len,0);
	}
	inline int sendClientSMystlModelTwo(const void* ps,int cliNum,int len)//model two
	{
		return send(psockClients[cliNum],(char*)ps,len,0);
	}
	void sendClientsEveryoneMystlTwo(const void* ps,int len)//model two
	{
		for(int i=0;i<numClient;i++)
		{ 
			if(psockClients[i]==0)
				continue;
			send(psockClients[i],(char*)ps,len,0);
		}
	}
	bool selectModelMysql(int* pthing,int* pnum,void* pget,int len,void* pneed,int (*pfunc)(int ,int ,int,void* ,void*,ServerTcpIp& ))
	{//pthing is 0 out,1 in,2 say pnum is the num of soc,pget is rec,len is the max len of pget,pneed is others things
		fd_set temp=fdClients;
		int sign=select(0,&temp,NULL,NULL,NULL);
		if(sign>0)
		{
			for(int i=0;i<(int)fd_count;i++)
			{
				if(FD_ISSET(fdClients.fds_bits[i],&temp))
				{
					if(fdClients.fds_bits[i]==sock)
					{
						if(fd_count<FD_SETSIZE)
						{
							sockaddr_in newaddr={0};
							int newClient=accept(sock,(sockaddr*)&newaddr,(socklen_t*)&sizeAddr);
							FD_SET(newClient,&fdClients);
							this->addFd(newClient);
							fd_count++;
							for(int j=0;j<(int)fd_count;j++)
							{
								if(newClient==fdClients.fds_bits[j])
								{
									*pnum=j;
									break;
								}
							}
							*pthing=1;
							strcpy((char*)pget,inet_ntoa(newaddr.sin_addr));
							if(pfunc!=NULL)
							{
								if(pfunc(*pthing,*pnum,0,pget,pneed,*this))
									return false;
							}
						}
						else
							continue;
					}
					else
					{
						int sRec=recv(fdClients.fds_bits[i],(char*)pget,len,0);
						*pnum=i;
						if(sRec>0)
						{
							*pthing=2;
						}
						if(sRec<=0)
						{
							close(fdClients.fds_bits[i]);
							this->deleteFd(fdClients.fds_bits[i]);
							FD_CLR(fdClients.fds_bits[i],&fdClients);
							fd_count--;
							*(char*) pget=0;
							*pthing=0;
						}
						if(pfunc!=NULL)
						{
							if(pfunc(*pthing,*pnum,sRec,pget,pneed,*this))
								return false;
						}
					}
				}
			}
		}
		else
			return false;
		return true;
	}
	inline int selectSendMystl(const void* ps,int cliNum,int len)//select model yo send
	{
		return send(fdClients.fds_bits[cliNum],(char*)ps,len,0);
	}
	void selectSendEveryoneMystl(void* ps,int len)//select model to send everyone
	{
		for(int i=0;i<fd_count;i++)
		{ 
			int clientSent=fdClients.fds_bits[i];
			if(clientSent!=0)
				send(fdClients.fds_bits[i],(char*)ps,len,0);
		}
	}
	bool updateSocketSelect(int* p,int* pcount)//get select array
	{
		if(fd_count!=0)
			*pcount=fd_count;
		else
			return false;
		for(int i=0;i<fd_count;i++)
			p[i]=fdClients.fds_bits[i];
		return true;
	}
	bool updateSocketEpoll(int* p,int* pcount)//get epoll array
	{
		if(fdNumNow!=0)
			*pcount=fdNumNow;
		else
			return false;
		for(int i=0;i<fdNumNow;i++)
			p[i]=pfdn[i];
		return true;
	}
	int sendSocketMystlSelect(int toClient,const void* ps,int len)
	{
		for(int i=0;i<fd_count;i++)
			if(toClient==fdClients.fds_bits[i])
				return send(fdClients.fds_bits[i],(char*)ps,len,0);
		return -1;
	}
    void sendEverySocket(void* ps,int len)
    {
        for(int i=0;i<fdNumNow;i++)
            if(pfdn[i]!=0)
                send(pfdn[i],ps,len,0);
    }
	inline int sendSocketAllModel(int socCli,const void* ps,int len)
	{
		return send(socCli,(char*)ps,len,0);
	}
	int findSocketSelsct(int i)
	{
		if(fdClients.fds_bits[i]!=0)
			return fdClients.fds_bits[i];
		else
			return -1;
	}
	bool findSocketEpoll(int cliSoc)
	{
		for(int i=0;i<fdNumNow;i++)
		{
			if(pfdn[i]==cliSoc)
				return true;
		}
		return false;
	}
	char* getHostName()
	{
		char name[30]={0};
		gethostname(name,30);
		memcpy(hostname,name,30);
		return hostname;
	}
	char* getHostIp()
	{
		char name[300]={0};
		gethostname(name,300);
		hostent* phost=gethostbyname(name);
		in_addr addr;
		char* p=phost->h_addr_list[0];
		memcpy(&addr.s_addr,p,phost->h_length);
		memset(hostip,0,sizeof(char)*200);
		memcpy(hostip,inet_ntoa(addr),strlen(inet_ntoa(addr)));
		return hostip;
	}
	bool epollModel(int* pthing,int* pnum,void* pget,int len,void* pneed,int (*pfunc)(int ,int ,int ,void* ,void*,ServerTcpIp& ))
	{
		int eventNum=epoll_wait(epfd,pevent,512,-1);
		for(int i=0;i<eventNum;i++)
		{
			epoll_event temp=pevent[i];
			if(temp.data.fd==sock)
			{
				sockaddr_in newaddr={0};
				int newClient=accept(sock,(sockaddr*)&newaddr,(socklen_t*)&sizeAddr);
                this->addFd(newClient);
				nowEvent.data.fd=newClient;
				nowEvent.events=EPOLLIN;
				epoll_ctl(epfd,EPOLL_CTL_ADD,newClient,&nowEvent);
				*pthing=1;
				*pnum=newClient;
				strcpy((char*)pget,inet_ntoa(newaddr.sin_addr));
				if(pfunc!=NULL)
				{
					if(pfunc(*pthing,*pnum,0,pget,pneed,*this))
						return false;
				}
			}
			else
			{
				int getNum=recv(temp.data.fd,(char*)pget,len,0);
				*pnum=temp.data.fd;
				if(getNum>0)
					*pthing=2;
				else
				{
					*(char*)pget=0;
					*pthing=0;
                    this->deleteFd(temp.data.fd);
					epoll_ctl(epfd,temp.data.fd,EPOLL_CTL_DEL,NULL);
					close(temp.data.fd);
				}
				if(pfunc!=NULL)
				{
					if(pfunc(*pthing,*pnum,getNum,pget,pneed,*this))
						return false;
				}
			}
		}
		return true;
	}
	char* getPeerIp(int cliSoc,int* pcliPort)
	{
		sockaddr_in cliAddr={0};
		int len=sizeof(cliAddr);
		if(-1==getpeername(cliSoc,(sockaddr*)&cliAddr,(socklen_t*)&len))
			return NULL;
		*pcliPort=cliAddr.sin_port;
		return inet_ntoa(cliAddr.sin_addr); 
	}
	bool disconnectSocketEpoll(int clisock)
    {
        close(clisock);
        return this->deleteFd(clisock);
    }
    void threadModel(void* pneed,void* (*pfunc)(void*))
    {
    	while(1)
    	{
			sockaddr_in newaddr={0};
			int newClient=accept(sock,(sockaddr*)&newaddr,(socklen_t*)&sizeAddr);
			if(newClient==-1)
				continue;
			this->addFd(newClient);
			ServerTcpIp::ArgvSer argv{*this,newClient,pneed};
			ThreadPoll::Task task={pfunc,&argv};
			pool->addTask(task);
		}
	}
	bool threadDeleteSoc(int clisoc)
	{
		close(clisoc);
		return this->deleteFd(clisoc);
	}
};
struct Msg{
	int flag;
	int len;
	char name[100];
};
void* func(void* argv)
{//1 send in,2 send out,0 find msg,4 wrong,3 delete,5 change name,6 memory ask,7 back to say ok
	Msg msg={0,0,{0}};
	char* pfile=NULL;
	ServerTcpIp& server=((ServerTcpIp::ArgvSer*)argv)->server;
	int soc=((ServerTcpIp::ArgvSer*)argv)->soc;
	printf("connect success\n");
	while(server.receiveMysqlAllModel(soc,&msg,sizeof(Msg))>0)
	{
		int len=0,all=0;
		bool flag=true;
		printf("%d %s %d\n",msg.flag,msg.name,msg.len);
		if(msg.flag!=0&&msg.flag!=1&&msg.flag!=2&&msg.flag!=3&&msg.flag!=5&&msg.flag!=6)
			continue;
		if(msg.flag==1)
		{
			if(pfile!=NULL)
				free(pfile);
			pfile=(char*)malloc(msg.len+10);
			memset(pfile,0,msg.len+10);
			while(msg.len-all>5)
			{
				len=server.receiveMysqlAllModel(soc,pfile+all,msg.len);
				if(len>0)
					all+=len;
				else 
				{
					flag=false;
					break;
				}
				printf("%d %d\n",all,msg.len);
			}
			if(false==flag)
				continue;
			FILE* fp=fopen(msg.name,"wb+");
			if(fp==NULL)
			{
				fp=fopen("GetNoNameFile","wb+");
				if(fp==NULL)
					continue;
			}
			for(int i=0;i<msg.len;i++)
				fputc(pfile[i],fp);
			fclose(fp);
			server.mutexLock();
			system("ls -t > log.txt");
			server.mutexUnlock();
			msg.flag=7;
			if(false==server.sendSocketAllModel(soc,&msg,sizeof(Msg)))
				continue;
			printf("%s ok\n",msg.name);
		}
		else if(msg.flag==2)
		{
			int i=0;
			FILE* fp=fopen(msg.name,"rb");
			if(fp==NULL)
			{
				msg.flag=4;
				if(-1==server.sendSocketAllModel(soc,&msg,sizeof(Msg)))
					continue;
				continue;
			}
			fseek(fp,0,SEEK_END);
			len=ftell(fp);
			fseek(fp,0,SEEK_SET);
			if(pfile!=NULL)
				free(pfile);
			pfile=(char*)malloc(sizeof(char)*len+10);
			if(pfile==NULL)
				continue;
			memset(pfile,0,sizeof(char)*len+10);
			for(i=0;i<len;i++)
				pfile[i]=fgetc(fp);
			pfile[i+1]=0;
			fclose(fp);
			msg.len=len;
			msg.flag=2;
			if(-1==server.sendSocketAllModel(soc,&msg,sizeof(Msg)))
				continue;
			sleep(1);
			if(-1==server.sendSocketAllModel(soc,pfile,len))
				continue;
			printf("%s send success\n",msg.name);
		}
		else if(msg.flag==0)
		{
			int i=0;
			server.mutexLock();
			FILE* fp=fopen("log.txt","r+");
			if(fp==NULL)
				fp=fopen("log.txt","w+");
			if(fp==NULL)
			{
				msg.flag=4;
				if(false==server.sendSocketAllModel(soc,&msg,sizeof(Msg)))
				{
					server.mutexUnlock();
					continue;
				}
				server.mutexUnlock();
				continue;
			}
			fseek(fp,0,SEEK_END);
			len=ftell(fp);
			fseek(fp,0,SEEK_SET);
			if(pfile!=NULL)
				free(pfile);
			pfile=(char*)malloc(sizeof(char)*len+10);
			if(pfile==NULL)
				continue;
			memset(pfile,0,sizeof(char)*len+10);
			for(i=0;i<len;i++)
				pfile[i]=fgetc(fp);
			pfile[i+1]=0;
			fclose(fp);
			server.mutexUnlock();
			msg.len=len;
			msg.flag=0;
			if(false==server.sendSocketAllModel(soc,&msg,sizeof(Msg)))
				continue;
			sleep(1);
			if(false==server.sendSocketAllModel(soc,pfile,len))
				continue;
		}
		else if(msg.flag==3)
		{
			FILE* fp=fopen(msg.name,"rb");
			if(fp==NULL)
			{
				msg.flag=4;
				if(false==server.sendSocketAllModel(soc,&msg,sizeof(Msg)))
					continue;
				continue;
			}
			fclose(fp);
			if(0==remove(msg.name))
			{
				server.mutexLock();
				system("ls -t > log.txt");
				server.mutexUnlock();
				msg.flag=3;
				if(false==server.sendSocketAllModel(soc,&msg,sizeof(Msg)))
					continue;
				continue;
			}
			else
			{
				msg.flag=4;
				if(false==server.sendSocketAllModel(soc,&msg,sizeof(Msg)))
					continue;
				continue;
			}
		}
		else if(msg.flag==5)
		{
			char oldName[100]={0},newName[100]={0};
			sscanf(msg.name,"%s%s",oldName,newName);
			FILE* fp=fopen(oldName,"rb+");
			if(fp==NULL)
			{
				msg.flag=4;
				if(false==server.sendSocketAllModel(soc,&msg,sizeof(Msg)))
					continue;
				continue;
			}
			fclose(fp);
			if(0==rename(oldName,newName))
			{
				server.mutexLock();
				system("ls -t > log.txt");
				server.mutexUnlock();
				msg.flag=5;
				if(false==server.sendSocketAllModel(soc,&msg,sizeof(Msg)))
					continue;
				continue;
			}
			else
			{
				msg.flag=4;
				if(false==server.sendSocketAllModel(soc,&msg,sizeof(Msg)))
					continue;
				continue;
			}
		}
		else if(msg.flag==6)
		{
			server.mutexLock();
			system("du -sh > sum");
			FILE* fp=fopen("./sum","r+");
			if(fp==NULL)
			{
				msg.flag=4;
				msg.len=0;
				server.mutexUnlock();
				if(false==server.sendSocketAllModel(soc,&msg,sizeof(Msg)))
					continue;
			}
			if(fscanf(fp,"%d",&msg.len)<=0)
			{
				msg.flag=4;
				msg.len=0;
				server.mutexUnlock();
				if(false==server.sendSocketAllModel(soc,&msg,sizeof(Msg)))
					continue;				
			}
			if(fscanf(fp,"%s",msg.name)<=0)
			{
				msg.flag=4;
				msg.len=0;
				server.mutexUnlock();
				if(false==server.sendSocketAllModel(soc,&msg,sizeof(Msg)))
					continue;				
			}
			if(strchr(msg.name,'M')==NULL)
				msg.len=0;
			msg.flag=6;
			server.mutexUnlock();
			if(false==server.sendSocketAllModel(soc,&msg,sizeof(Msg)))
				continue;
			fclose(fp);
		}
	}
	server.threadDeleteSoc(soc);
	return NULL;
}
void testTwo()
{
	
	int pid=0;
//	if((pid=fork())!=0)
//	{
//		printf("pid=%d",pid);
//		return;
//	}
	
	
	ServerTcpIp server(5209);
	if(false==server.bondhost())
	{
		printf("bound wrong\n");
		return;
	}
	if(false==server.setlisten())
	{
		printf("setlisten wrong\n");
		return;
	}
	printf("server ok\n");
	server.threadModel(NULL,func);
}
int main()
{
	testTwo();
}
